//
//  LoginViewController.swift
//  Copy_Aikaopu
//
//  Created by taojianz on 2017/6/27.
//  Copyright © 2017年 taojianz. All rights reserved.
//

import UIKit

class LoginViewController: BaseViewController {
    
    var topImageView : UIImageView!
    
    var userInputView: UIView!
    
    var loginBtn : UIButton!
    
    var registerBtn : UIButton!
    
    var wbBtn : UIButton!
    
    var wxBtn : UIButton!
    
    var qqBtn : UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "登录"
        
    }

    
}
extension LoginViewController{
    override func setupUI() {
        super.setupUI()
        
        setupTopImage()
        setupInputView()
        setupLoginBtn()
        setupRegisterBtn()
        setupBottomView()
    }
    override func setupNav() {
        
        navigationController?.isNavigationBarHidden = false
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        
        // TODO: 完成忘记密码的转场
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "忘记密码", style: .plain, target: self, action: nil)

        navigationController?.navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 99)], for: .normal)
        
        navigationController?.navigationBar.tintColor = .red
    }
    func setupTopImage(){
        topImageView = UIImageView(image: #imageLiteral(resourceName: "jiehun"), highlightedImage: nil)
        topImageView.sizeToFit()
        
        view.addSubview(topImageView)
        
        topImageView.snp.makeConstraints { (maker) in
            maker.top.equalToSuperview().offset(84)
            maker.centerX.equalTo(view.snp.centerX).offset(0)
        }
        
    }
    func setupInputView(){
        userInputView = UIView()
        
        userInputView.backgroundColor = .lightGray
        userInputView.layer.cornerRadius = 5
        view.addSubview(userInputView)
        
        // 账号输入框
        let userAccountInputField = UITextField()
        
        userAccountInputField.borderStyle = .none
        userAccountInputField.placeholder = "账号"
        
        userInputView.addSubview(userAccountInputField)
        
        userAccountInputField.snp.makeConstraints { (maker) in
            maker.width.equalTo(userInputView).offset(-80)
            maker.top.equalTo(userInputView.snp.top).offset(8)
            maker.centerX.equalTo(userInputView.snp.centerX)
            maker.height.equalTo(32)
        }
        
        // 登录框图标
        let userAccountIcon = UIImageView(image: #imageLiteral(resourceName: "dengluyonghu"))
        userAccountIcon.sizeToFit()
        
        userInputView.addSubview(userAccountIcon)
        
        userAccountIcon.snp.makeConstraints { (maker) in
            maker.right.equalTo(userAccountInputField.snp.left).offset(-8)
            maker.centerY.equalTo(userAccountInputField.snp.centerY)
        }
        // 灰色线条
        let grayLine = UIView()
        grayLine.backgroundColor = .darkGray
        
        userInputView.addSubview(grayLine)
        
        grayLine.snp.makeConstraints { (maker) in
            maker.height.equalTo(1)
            maker.left.equalTo(userAccountIcon.snp.left)
            maker.right.equalTo(userAccountInputField.snp.right).offset(userAccountIcon.frame.width)
            maker.top.equalTo(userAccountInputField.snp.bottom).offset(4)
        }
        
        
        // 密码输入框
        let psdInputField = UITextField()
        
        psdInputField.borderStyle = .none
        psdInputField.placeholder = "密码"
        
        userInputView.addSubview(psdInputField)
        
        psdInputField.snp.makeConstraints { (maker) in
            maker.size.equalTo(userAccountInputField)
            maker.centerX.equalTo(userAccountInputField.snp.centerX)
            maker.top.equalTo(userAccountInputField.snp.bottom).offset(8)
        }
        
        // 密码输入框的图标
        let psdFieldIcon = UIImageView(image: #imageLiteral(resourceName: "mima"))
        psdInputField.sizeToFit()
        
        userInputView.addSubview(psdFieldIcon)
        
        psdFieldIcon.snp.makeConstraints { (maker) in
            maker.right.equalTo(psdInputField.snp.left).offset(-8)
            maker.centerY.equalTo(psdInputField.snp.centerY)
        }
        
        
        // in
        userInputView.snp.makeConstraints { (maker) in
            maker.top.equalTo(topImageView.snp.bottom)
            maker.centerX.equalTo(topImageView.snp.centerX)
            
            maker.left.equalToSuperview().offset(30)
            maker.right.equalToSuperview().offset(-30)
            
            maker.bottom.equalTo(psdInputField.snp.bottom).offset(3)
        }
        
    }
    
    func setupLoginBtn(){
        // TODO: 登录按钮点击事件
        self.loginBtn = UIButton(type: .system)
        
        loginBtn.backgroundColor = .red
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.setTitleColor(.white, for: .normal)
        loginBtn.layer.cornerRadius = 5
        
        view.addSubview(loginBtn)
        
        loginBtn.snp.makeConstraints { (maker) in
            maker.top.equalTo(userInputView.snp.bottom).offset(48)
            maker.width.equalTo(userInputView.snp.width).offset(10)
            maker.centerX.equalTo(userInputView.snp.centerX)
            maker.height.equalTo(40)
        }
    }

    func setupRegisterBtn(){
        // TODO: 注册按钮点击事件
        self.registerBtn = UIButton(type: .system)
        
        registerBtn.setTitleColor(.black, for: .normal)
        registerBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        registerBtn.setTitle("立即注册", for: .normal)
        registerBtn.backgroundColor = view.backgroundColor
        
        view.addSubview(registerBtn)
        
        registerBtn.snp.makeConstraints { (maker) in
            maker.top.equalTo(loginBtn.snp.bottom).offset(16)
            maker.centerX.equalToSuperview()
        }
        registerBtn.addTarget(self, action: #selector(registerBtnTapped), for: .touchUpInside)
    
    }
    func setupBottomView(){
        let lineView = UIView()
        
        lineView.backgroundColor = .darkGray
        
        view.addSubview(lineView)
        
        lineView.snp.makeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.width.equalTo(200)
            maker.top.equalTo(registerBtn.snp.bottom).offset(150)
            maker.height.equalTo(1)
        }
        
        let txtView = UITextField()
        
        txtView.font = UIFont.systemFont(ofSize: 11)
        txtView.text = "第三方账号快捷登录"
        txtView.backgroundColor = view.backgroundColor
        txtView.isEnabled = false
        
        view.addSubview(txtView)
        
        txtView.snp.makeConstraints { (maker) in
            maker.centerX.equalTo(lineView)
            maker.centerY.equalTo(lineView)
        }
        
        // 三方登录按钮
        // 微博按钮
        // TODO: 第三方登录按钮的点击事件
        self.wbBtn = UIButton()
        
        wbBtn.setImage(#imageLiteral(resourceName: "weibo"), for: .normal)
        wbBtn.setImage(#imageLiteral(resourceName: "weiboxuanzhong"), for: .highlighted)
        
        view.addSubview(wbBtn)
        
        wbBtn.snp.makeConstraints { (maker) in
            maker.left.equalToSuperview().offset(view.bounds.width/7)
            maker.top.equalTo(txtView.snp.bottom).offset(18)
        }
        // 微信按钮
        self.wxBtn = UIButton()
        
        wxBtn.setImage(#imageLiteral(resourceName: "weixin"), for: .normal)
        wxBtn.setImage(#imageLiteral(resourceName: "weixinxuanzhong"), for: .highlighted)
        
        view.addSubview(wxBtn)
        
        wxBtn.snp.makeConstraints { (maker) in
            maker.left.equalToSuperview().offset(view.bounds.width/7*3)
            maker.centerY.equalTo(wbBtn.snp.centerY)
        }
        // QQ按钮
        self.qqBtn = UIButton()
        
        qqBtn.setImage(#imageLiteral(resourceName: "QQ"), for: .normal)
        qqBtn.setImage(#imageLiteral(resourceName: "QQxuanzhong"), for: .highlighted)
        
        view.addSubview(qqBtn)
        
        qqBtn.snp.makeConstraints { (maker) in
            maker.left.equalToSuperview().offset(view.bounds.width/7*5)
            maker.centerY.equalTo(wbBtn.snp.centerY)
        }
        
    }
}

// MARK: 按钮的点击事件
extension LoginViewController{
    func registerBtnTapped(){
        let registerVC1 = RegisterViewController()
        navigationController?.pushViewController(registerVC1, animated: true)
    }
}
